<script setup>
import { ref, computed } from "vue";
import ProductView from "../components/ProductView.vue";
import products from "../assets/product";

let filterBrand = ref("");

const brands = computed(() => {
  const brand = products.map((item) => item.brand);
  return getFilterArray(brand);
});

function handleFilterBrand(brand) {
  if (filterBrand.value === brand) {
    filterBrand.value = "";
  } else {
    filterBrand.value = brand;
  }
}

let filterColor = ref("");

const colors = computed(() => {
  const color = products.map((item) => item.color);
  return getFilterArray(color);
});

function handleFilterColor(color) {
  if (filterColor.value === color) {
    filterColor.value = "";
  } else {
    filterColor.value = color;
  }
}

let order = ref("");

function handleOrderDefault() {
  order.value = "";
}

function handleOrderSales() {
  order.value = "sales";
}

function handleOrderCost() {
  if (order.value === "cost-desc") {
    order.value = "cost-asc";
  } else {
    order.value = "cost-desc";
  }
}

const filteredAndOrderedList = computed(() => {
  let list = [...products];
  // 按品牌过滤
  if (filterBrand.value !== "") {
    list = list.filter((item) => item.brand === filterBrand.value);
  }
  // 按颜色过滤
  if (filterColor.value !== "") {
    list = list.filter((item) => item.color === filterColor.value);
  }
  // 排序
  if (order.value !== "") {
    if (order.value === "sales") {
      list = list.sort((a, b) => b.sales - a.sales);
    } else if (order.value === "cost-desc") {
      list = list.sort((a, b) => b.cost - a.cost);
    } else if (order.value === "cost-asc") {
      list = list.sort((a, b) => a.cost - b.cost);
    }
  }
  return list;
});

// 数组排重
function getFilterArray(array) {
  const res = [];
  const json = {};
  for (let i = 0; i < array.length; i++) {
    const _self = array[i];
    if (!json[_self]) {
      res.push(_self);
      json[_self] = 1;
    }
  }
  return res;
}
</script>

<template>
  <div class="list-control">
    <div class="list-control-filter">
      <span>品牌：</span>
      <span
        class="list-control-filter-item"
        :class="{ on: item === filterBrand }"
        v-for="item in brands"
        @click="handleFilterBrand(item)"
      >
        {{ item }}
      </span>
    </div>
    <div class="list-control-filter">
      <span>颜色：</span>
      <span
        class="list-control-filter-item"
        :class="{ on: item === filterColor }"
        v-for="item in colors"
        @click="handleFilterColor(item)"
      >
        {{ item }}
      </span>
    </div>
    <div class="list-control-order">
      <span>排序：</span>
      <span
        class="list-control-order-item"
        :class="{ on: order === '' }"
        @click="handleOrderDefault"
      >
        默认
      </span>
      <span
        class="list-control-order-item"
        :class="{ on: order === 'sales' }"
        @click="handleOrderSales"
      >
        销量
        <template v-if="order === 'sales'">↓</template>
      </span>
      <span
        class="list-control-order-item"
        :class="{ on: order.indexOf('cost') > -1 }"
        @click="handleOrderCost"
      >
        价格
        <template v-if="order === 'cost-asc'">↑</template>
        <template v-if="order === 'cost-desc'">↓</template>
      </span>
    </div>
  </div>
  <product-view
    v-for="item in filteredAndOrderedList"
    :info="item"
    :key="item.id"
  ></product-view>
  <div class="product-not-found" v-show="!filteredAndOrderedList.length">
    暂无相关商品
  </div>
</template>

<style scoped>
.list-control {
  background: #fff;
  border-radius: 6px;
  margin: 16px;
  padding: 16px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}

.list-control-filter {
  margin-bottom: 16px;
}

.list-control-filter-item,
.list-control-order-item {
  cursor: pointer;
  display: inline-block;
  border: 1px solid #e9eaec;
  border-radius: 4px;
  margin-right: 6px;
  padding: 2px 6px;
}

.list-control-filter-item.on,
.list-control-order-item.on {
  background: #f2352e;
  border: 1px solid #f2352e;
  color: #fff;
}

.product-not-found {
  text-align: center;
  padding: 32px;
}
</style>
